package com.bzchao.netty.udp.consumer;

import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;

@Slf4j
public class StringUdpConsumer implements UdpConsumer {
    /**
     * 接收消息的编码
     */
    private Charset revCharset = Charset.defaultCharset();

    public void setRevCharset(Charset msgCharset) {
        this.revCharset = msgCharset;
    }

    @Override
    public void accept(ByteBuf buf) {
        CharSequence charSequence = buf.readCharSequence(buf.readableBytes(), revCharset);
        String content = String.valueOf(charSequence);
        this.accept(content);
    }

    /**
     * 接收消息-字符串消息
     *
     * @param msg 消息内容
     */
    public void accept(String msg) {
        log.info("客户端消息==>：\n{}", msg);
    }
}
